package com.niuwa.p2p.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class JsonUtil {

	/**
	 *  WriteDateUseDateFormat 日期类型数据的格式化，包括java.util.Date、java.sql.Date、java.sql.Timestamp、java.sql.Time。
	 *  	defaultformat: yyyy-MM-dd HH:mm:ss
	 *  WriteClassName 输出Class
	 *  	JSON.toJSONString(color, SerializerFeature.WriteClassName); 
	 *      {"@type":"java.awt.Color","r":255,"g":0,"b":0,"alpha":255}
	 *  BrowserCompatible 浏览器和设备兼容
	 *  	比如支持 iphone上的emoji（绘文字）
	 */
	//private static final SerializerFeature[] SERIALIZER_FEATURE_DEFAULT = {SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteClassName,SerializerFeature.BrowserCompatible};
	
	private static final SerializerFeature[] SERIALIZER_FEATURE_DEFAULT = {SerializerFeature.WriteDateUseDateFormat, SerializerFeature.BrowserCompatible};
	
	public static String toJSONString(Object data){				
		return JSON.toJSONString(data);		
	}
		
	public static String toJSONStringWithSerializerFeature(Object data){				
		return JSON.toJSONString(data, SERIALIZER_FEATURE_DEFAULT);		
	}
	
	public static String toJSONStringWithSerializerFeature(Object data, SerializerFeature[] features){				
		return JSON.toJSONString(data, features);		
	}
	
	public static Object parseObject(String text){
		return JSON.parseObject(text);		
	}
	

	@SuppressWarnings("unchecked")
	public static <T> T getForm(String jsonString, Class<?> cls) {
	    T t = null;
	    try {
	        t = (T) JSON.parseObject(jsonString, cls);
	    } catch (Exception e) {
	     
	    }
	    return t;
	}

	
	
}
